package com.zenithmind.task.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.task.pojo.entity.TaskAssignment;
import com.zenithmind.task.pojo.query.TaskAssignmentQuery;
import com.zenithmind.task.pojo.vo.TaskAssignmentVO;
import com.zenithmind.task.service.TaskAssignmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 任务分配管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/task-assignment")
@RequiredArgsConstructor
@Tag(name = "任务分配管理", description = "任务分配的创建、查询、接受、拒绝等功能")
public class TaskAssignmentController {

    private final TaskAssignmentService taskAssignmentService;

    // ==================== 基础CRUD操作 ====================

    @GetMapping("/page")
    @Operation(summary = "分页查询任务分配列表", description = "支持多条件筛选的任务分配分页查询")
    public Result<PageResult<TaskAssignmentVO>> getTaskAssignmentPage(TaskAssignmentQuery query) {
        String userId = UserContext.requireUserId();
        PageResult<TaskAssignmentVO> result = taskAssignmentService.getTaskAssignmentPage(query, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询任务分配详情", description = "根据ID查询任务分配的详细信息")
    public Result<TaskAssignmentVO> getTaskAssignmentById(@Parameter(description = "分配ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        TaskAssignmentVO taskAssignment = taskAssignmentService.getTaskAssignmentById(id, userId);
        return Result.success(taskAssignment);
    }

    @PostMapping
    @Operation(summary = "创建任务分配", description = "创建新的任务分配")
    public Result<Boolean> createTaskAssignment(@Valid @RequestBody TaskAssignment taskAssignment) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.createTaskAssignment(taskAssignment, userId);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新任务分配", description = "更新任务分配信息")
    public Result<Boolean> updateTaskAssignment(@Valid @RequestBody TaskAssignment taskAssignment) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.updateTaskAssignment(taskAssignment, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除任务分配", description = "删除指定的任务分配")
    public Result<Boolean> deleteTaskAssignment(@Parameter(description = "分配ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.deleteTaskAssignment(id, userId);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除任务分配", description = "批量删除多个任务分配")
    public Result<Boolean> batchDeleteTaskAssignments(@RequestBody List<String> ids) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.batchDeleteTaskAssignments(ids, userId);
        return Result.success(result);
    }

    // ==================== 分配操作 ====================

    @PutMapping("/{id}/accept")
    @Operation(summary = "接受任务分配", description = "接受分配给自己的任务")
    public Result<Boolean> acceptTaskAssignment(@Parameter(description = "分配ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.acceptTaskAssignment(id, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/reject")
    @Operation(summary = "拒绝任务分配", description = "拒绝分配给自己的任务")
    public Result<Boolean> rejectTaskAssignment(
            @Parameter(description = "分配ID") @PathVariable String id,
            @Parameter(description = "拒绝原因") @RequestParam String reason) {
        String userId = UserContext.requireUserId();
        Boolean result = taskAssignmentService.rejectTaskAssignment(id, reason, userId);
        return Result.success(result);
    }

    // ==================== 查询方法 ====================

    @GetMapping("/user/{userId}")
    @Operation(summary = "查询用户的任务分配", description = "查询指定用户的任务分配列表")
    public Result<List<TaskAssignmentVO>> getUserTaskAssignments(
            @Parameter(description = "用户ID") @PathVariable String userId,
            @Parameter(description = "分配状态") @RequestParam(required = false) Integer status) {
        String currentUserId = UserContext.requireUserId();
        List<TaskAssignmentVO> result = taskAssignmentService.getUserTaskAssignments(userId, status);
        return Result.success(result);
    }

    @GetMapping("/my")
    @Operation(summary = "查询我的任务分配", description = "查询当前用户的任务分配列表")
    public Result<List<TaskAssignmentVO>> getMyTaskAssignments(
            @Parameter(description = "分配状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.requireUserId();
        List<TaskAssignmentVO> result = taskAssignmentService.getUserTaskAssignments(userId, status);
        return Result.success(result);
    }

    @GetMapping("/task/{taskId}")
    @Operation(summary = "查询任务的分配列表", description = "查询指定任务的所有分配记录")
    public Result<List<TaskAssignmentVO>> getTaskAssignments(
            @Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        List<TaskAssignmentVO> result = taskAssignmentService.getTaskAssignments(taskId, userId);
        return Result.success(result);
    }

    @GetMapping("/my/pending")
    @Operation(summary = "查询我的待处理分配", description = "查询分配给当前用户且待接受的任务")
    public Result<List<TaskAssignmentVO>> getMyPendingAssignments() {
        String userId = UserContext.requireUserId();
        List<TaskAssignmentVO> result = taskAssignmentService.getUserTaskAssignments(userId, 1); // 待接受状态
        return Result.success(result);
    }

    @GetMapping("/my/accepted")
    @Operation(summary = "查询我已接受的分配", description = "查询当前用户已接受的任务分配")
    public Result<List<TaskAssignmentVO>> getMyAcceptedAssignments() {
        String userId = UserContext.requireUserId();
        List<TaskAssignmentVO> result = taskAssignmentService.getUserTaskAssignments(userId, 2); // 已接受状态
        return Result.success(result);
    }
}
